﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Text.RegularExpressions;
using System.Data.Common;

namespace ZhhbSoft.Data
{
    /// <summary>
    ///OleDbDbProvider 的摘要说明
    /// </summary>
    public class OleDbDbProvider : DbProvider
    {
        public OleDbDbProvider()
        {

        }
        /// <summary>
        /// 创建指定连接串的实例
        /// </summary>
        /// <param name="strConnectionString"></param>
        public OleDbDbProvider(string strConnectionString)
            : base(strConnectionString)
        {
        }
        public override string DbDateFunc
        {
            get
            {
                return "now()";
            }
        }
        public override string DbDateVar(string datetimeValue)
        {
            return "#" + datetimeValue + "#";
        }
        public override string DbDateAdd(string unit, int minuend, string datetimeValue)
        {
            return "dateadd('" + unit + "', " + minuend + ", " + datetimeValue + ")";
        }
        public override string DbDateDiff(string unit, string datetimeValue1, string datetimeValue2)
        {
            return "datediff('" + unit + "', " + datetimeValue1 + ", " + datetimeValue2 + ")";
        }
        public override string GetDeleteCommandText(string tableName)
        {
            return "delete * from [" + tableName + "]";

        }

        protected override IDbConnection CreateConnection()
        {
            try
            {
                return new OleDbConnection();
            }
            catch
            {
                throw new Exception("数据库连接失败！");
            }
        }

        protected override DbDataAdapter CreateDataAdapter(IDbCommand cmd)
        {
            return new OleDbDataAdapter((OleDbCommand)cmd);
        }

        protected override IDbDataParameter CreateDataParameter()
        {
            var sqlParameter = new OleDbParameter();
            return sqlParameter;
        }
        public override string SqlEncode(object value, DbType dbType)
        {
            if (value == null)
            {
                return "null";
            }
            switch (dbType)
            {
                case DbType.String:
                    {
                        return "'" + value.ToString().Replace("'", "''") + "'";
                    }
                case DbType.Int32:
                    {
                        return Convert.ToInt32(value).ToString();
                    }
                case DbType.Date:
                    {
                        return "#" + Convert.ToDateTime(value).ToString("MM-dd-yyyy") + "#";
                    }
                case DbType.Time:
                    {
                        return "#" + Convert.ToDateTime(value).ToString("HH:mm:ss") + "#";
                    }
                case DbType.DateTime:
                    {
                        return "#" + Convert.ToDateTime(value).ToString("MM-dd-yyyy HH:mm:ss") + "#";
                    }
                case DbType.Boolean:
                    {
                        bool boolTemp;
                        return bool.TryParse(value.ToString(), out boolTemp)
                                   ? (boolTemp ? "-1" : "0")
                                   : (value.ToString() == "0" ? "0" : "-1");
                    }
                default:
                    {
                        return "'" + value.ToString().Replace("'", "''") + "'";
                    }
            }
        }

        protected override void RebulidCommandParameter(IDbCommand command)
        {
            if (command.CommandType == CommandType.Text)
            {
                string strCommandText = command.CommandText;
                for (int i = command.Parameters.Count - 1; i >= 0; i--)
                {
                    var parameter = (IDbDataParameter)command.Parameters[i];
                    var regex = new Regex(string.Format(@"({0}(?=[,)\s;])|{0}$)", parameter.ParameterName.Trim()), RegexOptions.IgnoreCase);

                    strCommandText = regex.Replace(strCommandText, SqlEncode(parameter.Value, parameter.DbType));
                    command.Parameters.RemoveAt(parameter.ParameterName);
                }
                command.CommandText = strCommandText;
            }

        }

        public override IDbCommand CreateCommand()
        {
            return new OleDbCommand();
        }
    }
}